home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Camelot / Camelot 043 (1989-06)(Swedish User Group of Amiga)(SE)(PD)[WB].zip / Camelot 043 (1989-06)(Swedish User Group of Amiga)(SE)(PD)[WB].adf / zc / decl.c < prev    next >
C/C++ Source or Header  |  1989-03-08  |  11KB  |  604 lines

  1. /* Copyright (c) 1988 by Sozobon, Limited.  Author: Johann Ruegg
  2.  *
  3.  * Permission is granted to anyone to use this software for any purpose
  4.  * on any computer system, and to redistribute it freely, with the
  5.  * following restrictions:
  6.  * 1) No charge may be made other than reasonable charges for reproduction.
  7.  * 2) Modified versions must be clearly marked as such.
  8.  * 3) The authors are not responsible for any harmful consequences
  9.  *    of using this software, even if they result from defects in it.
  10.  *
  11.  *    decl.c
  12.  *
  13.  *    Do all declarations
  14.  *
  15.  *    Currently,
  16.  *        struct tags are local
  17.  *        struct members are tied to the struct
  18.  *        enum tags are ignored
  19.  *        enum members are local
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include "param.h"
  24. #include "tok.h"
  25. #include "nodes.h"
  26.  
  27. extern NODE *cur;
  28. extern level;
  29.  
  30. NODEP symtab[NHASH], tagtab;
  31. extern NODE *blktab;
  32.  
  33. NODEP alltags(), allsyms(), llook(), hlook();
  34.  
  35. extern int oflags[];
  36. #define debug    oflags['v'-'a']
  37.  
  38. /* look for global data decls
  39.     return when see something weird
  40.     return last ID declared */
  41. NODEP
  42. glb_decls()
  43. {
  44.     register NODEP head, xp;
  45.     NODEP d_type(), def_type(), d_declr();
  46.     int sclass;
  47.  
  48.     for(;;) {
  49.         sclass = d_scl(HERE_SC);
  50.         head = d_type();
  51.         if (head == NULL)
  52.             head = def_type();
  53.         if (ok_gsh(sclass, head) == 0)
  54.             continue;
  55.     more:
  56.         xp = d_declr(head,0);
  57.         if (ok_gx(xp,head)) {
  58.             xp->e_sc = sclass;
  59.             opt_ginit(xp);
  60.             new_sym(symtab,xp);
  61.             if (xp->n_tptr->t_token == '(') {       /* func */
  62.                 if (cur->e_token == ',' ||
  63.                     cur->e_token == ';')
  64.                     fix_fun(xp);
  65.                 else
  66.                     return xp;
  67.             }
  68.         }
  69.  
  70.         if (cur->e_token == ',') {
  71.             fadvnode();
  72.             goto more;
  73.         }
  74.  
  75.         if (cur->e_token == ';') {
  76.             fadvnode();
  77.         } else
  78.             return NULL;
  79.     }
  80. }
  81.  
  82. /* do local or arg decls
  83.     return 1 if see something */
  84. loc_decls()
  85. {
  86.     register NODEP head, xp;
  87.     NODEP d_type(), def_type(), d_declr();
  88.     int sclass;
  89.     int regs;
  90.     long size;
  91.     int rv = 0;
  92.  
  93.     size = level > 2 ? blktab->n_next->b_size : 0;
  94.     regs = level > 1 ? blktab->n_next->b_regs : 0;
  95.     while (is_ty_start()) {
  96.         rv++;
  97.         sclass = d_scl(K_AUTO);
  98.         head = d_type();
  99.         if (head == NULL)
  100.             head = def_type();
  101.         if (ok_lsh(sclass, head) == 0)
  102.             continue;
  103.     more:
  104.         xp = d_declr(head,0);
  105.         if (ok_lx(xp,head)) {
  106.             xp->e_sc = sclass;
  107.             if (level > 1)  /* not args */
  108.                 lc_size(&size, ®s, xp);
  109.             new_sym(&blktab->b_syms,xp);
  110.             fix_fun(xp);
  111.             opt_linit(xp,sclass);
  112.         }
  113.  
  114.         if (cur->e_token == ',') {
  115.             fadvnode();
  116.             goto more;
  117.         }
  118.  
  119.         if (cur->e_token == ';') {
  120.             fadvnode();
  121.         } else {
  122.             error("expect ;");
  123.             return 1;
  124.         }
  125.     }
  126.     while (STACKALN & size)
  127.         size++;
  128.     blktab->b_size = size;
  129.     blktab->b_regs = regs;
  130.     return rv;
  131. }
  132.  
  133. /* Decls inside Struct/Union */
  134. su_decls(listpp, isunion, sizep, alnp)
  135. NODEP *listpp;
  136. long *sizep;
  137. char *alnp;
  138. {
  139.     register NODEP head, xp;
  140.     NODEP d_type(), d_declr();
  141.     long size;
  142.     char aln;
  143.     int fldw, fldoff;
  144.  
  145.     aln = 0;
  146.     size = 0;
  147.     fldoff = 0;
  148.     for(;;) {
  149.         head = d_type();
  150.         if (head == NULL)
  151.             goto out;
  152.         if (ok_suh(head) == 0)
  153.             continue;
  154.     more:
  155.         xp = d_declr(head,0);
  156.         opt_field(xp,&fldw,isunion);
  157.         if (ok_sux(xp,head)) {
  158.             if (fldw > 0) { /* handle field */
  159.                 su_fld(&size,&aln,xp,fldw,&fldoff);
  160.                 xp->e_offs = size;
  161.             } else {        /* handle non-field */
  162.                 afterfld(&size,&fldoff);
  163.                 xp->e_offs = isunion ? 0 : size;
  164.                 su_size(&size,&aln,xp,isunion);
  165.             }
  166.             new_sym(listpp,xp);
  167.             listpp = &xp->n_next;
  168.         } else if (fldw == 0) {
  169.             afterfld(&size, &fldoff);
  170.         }
  171.  
  172.         if (cur->e_token == ',') {
  173.             fadvnode();
  174.             goto more;
  175.         }
  176.  
  177.         if (cur->e_token == ';') {
  178.             fadvnode();
  179.         } else
  180.             goto out;
  181.     }
  182. out:
  183.     afterfld(&size,&fldoff);
  184.     while (aln & size)
  185.         size++;
  186.     *sizep = size;
  187.     *alnp = aln;
  188.     return;
  189. }
  190.  
  191. /* Decls inside Enum */
  192. en_decls()
  193. {
  194.     register NODEP head, xp;
  195.     NODEP bas_type(), d_declr();
  196.     int curval = 0;
  197.  
  198.     for(;;) {
  199.         head = bas_type(K_INT);
  200.     more:
  201.         xp = d_declr(head,0);
  202.         if (ok_enx(xp,head)) {
  203.             opt_enval(&curval);
  204.             xp->e_ival = curval++;
  205.             xp->e_sc = ENUM_SC;
  206.             new_sym(level ? blktab->b_syms : (NODE *)symtab,
  207.                 xp);
  208.         }
  209.  
  210.         if (cur->e_token == ',') {
  211.             fadvnode();
  212.             goto more;
  213.         }
  214.  
  215.         return;
  216.     }
  217. }
  218.  
  219. /*
  220.  * called from expr.c, make a cast
  221.  * only called if is_ty_start();
  222.  */
  223. NODE *
  224. makecast()
  225. {
  226.     NODEP head, xp;
  227.     register NODEP np;
  228.     NODEP d_type(), d_declr(), def_type();
  229.  
  230.     head = d_type();        /* we know this is not NULL */
  231.     xp = d_declr(head, 1);
  232.     if (ok_cast(xp,head) == 0) {
  233.         xp = def_type();        /* return cast to INT */
  234.     }
  235.     np = allocnode();
  236.     np->e_token = TCONV;
  237.     np->n_tptr = xp;
  238.     if (xp == head)
  239.         np->n_flags |= N_COPYT;
  240.     if (debug) {
  241.         printf("Make cast");
  242.         printnode(np);
  243.     }
  244.     return np;
  245. }
  246.  
  247. is_ty_start()
  248. {
  249.     NODEP rv;
  250.  
  251.     if (is_tykw(cur->e_token))
  252.         return 1;
  253.     if (cur->e_token == ID) {
  254.         rv = allsyms(cur);
  255.         if (rv && rv->e_sc == K_TYPEDEF)
  256.             return 1;
  257.     }
  258.     return 0;
  259. }
  260.  
  261. /* assemble decl and put in listpp */
  262. new_sym(listpp, xp)
  263. NODEP *listpp;
  264. NODEP xp;
  265. {
  266.     NODEP old;
  267.  
  268.     if (xp == NULL)
  269.         return 0;
  270. /* put in table */
  271.     if (debug) {
  272.         printf("New sym sc %c", "EARTSCH"[xp->e_sc-K_EXTERN]);
  273.         printnode(xp);
  274.     }
  275.     /* later look for previous definition */
  276.     if (listpp == (NODE **)symtab) {
  277.         old = hlook(listpp, xp);
  278.         if (old == NULL || def2nd(old, xp))
  279.             puthlist(listpp, xp);
  280.     } else {
  281.         old = llook(*listpp, xp);
  282.         if (old == NULL || def2nd(old, xp))
  283.             putlist(listpp, xp);
  284.     }
  285.     return 1;
  286. }
  287.  
  288. /* look for storage class */
  289. d_scl(defau)
  290. {
  291.     int rv;
  292.  
  293.     if (is_sclass(cur->e_token)) {
  294.         rv = cur->e_token;
  295.         fadvnode();
  296.         return rv;
  297.     }
  298.     /* no storage class specified */
  299.     return defau;
  300. }
  301.  
  302. NODEP
  303. d_declr(head, forcast)
  304. NODEP head;
  305. {
  306.     NODEP e1;
  307.     NODEP declarator(), rev_decl();
  308.     NODEP xp, tailp;
  309.  
  310.     e1 = declarator();
  311.     xp = rev_decl(e1, &tailp, forcast);
  312.     if (xp) {
  313.         tailp->n_tptr = head;
  314.         tailp->n_flags |= N_COPYT;
  315.         return xp;
  316.     } else if (forcast)
  317.         return head;
  318.     else
  319.         return NULL;
  320. }
  321.  
  322. NODEP
  323. rev_decl(np,tailpp,forcast)
  324. NODEP np, *tailpp;
  325. {
  326.     NODEP rv, scan, nxt;
  327.  
  328.     rv = NULL;
  329.     for (scan = np; scan != NULL; scan = nxt) {
  330.         nxt = scan->n_next;
  331.         scan->n_next = NULL;
  332.         if (rv == NULL) {
  333.             *tailpp = scan;
  334.             scan->n_tptr = NULL;
  335.             rv = scan;
  336.         } else {
  337.             scan->n_tptr = rv;
  338.             rv = scan;
  339.         }
  340.         e_to_t(rv);
  341.         switch (rv->t_token) {
  342.         case UNARY '*':
  343.             sprintf(rv->n_name, "Ptr to");
  344.             break;
  345.         case '(':
  346.             sprintf(rv->n_name, "Fun ret");
  347.             break;
  348.         case '[':
  349.             sprintf(rv->n_name, "Ary of");
  350.             break;
  351.         case ID:
  352.             break;
  353.         default:
  354.             error("bad type xpr");
  355.             return NULL;
  356.         }
  357.     }
  358.     /* if normal decl and see something, must see id first */
  359.     if (!ok_revx(rv,forcast))
  360.         rv = NULL;
  361.     return rv;
  362. }
  363.  
  364. /*
  365.  * Looking for type part of a decl
  366.  */
  367. NODEP
  368. d_type()
  369. {
  370.     int btype, adj;
  371.     NODEP rv;
  372.     NODEP bas_type(), decl_su(), decl_enum();
  373.  
  374.     /* look for 'struct', 'union', 'enum' or typedef ID */
  375.     switch (cur->e_token) {
  376.     case ID:
  377.         rv = allsyms(cur);
  378.         if (rv && rv->e_sc == K_TYPEDEF) {
  379.             fadvnode();
  380.             rv = rv->n_tptr;
  381.             return rv;
  382.         }
  383.         return NULL;
  384.     case K_STRUCT:
  385.         return decl_su(0);
  386.     case K_UNION:
  387.         return decl_su(1);
  388.     case K_ENUM:
  389.         return decl_enum();
  390.     }
  391.  
  392.     /* look for modifiers 'long', 'short', 'unsigned' */
  393.     adj = 0;
  394.     while (is_tadj(cur->e_token)) {
  395.         switch (cur->e_token) {
  396.         case K_SHORT:
  397.             adj |= SAW_SHORT;
  398.             break;
  399.         case K_LONG:
  400.             adj |= SAW_LONG;
  401.             break;
  402.         case K_UNSIGNED:
  403.             adj |= SAW_UNS;
  404.             break;
  405.         }
  406.         fadvnode();
  407.     }
  408.  
  409.     /* look for base type 'char', 'int', 'float', 'double', 'void'*/
  410.     if (is_btype(cur->e_token)) {
  411.         btype = cur->e_token;
  412.         fadvnode();
  413.     } else if (adj == 0)    /* saw nothing */
  414.         return NULL;
  415.     else
  416.         btype = K_INT;
  417.  
  418.     if (adj)
  419.         btype = adj_type(btype, adj);
  420.     rv = bas_type(btype);
  421.     return rv;
  422. }
  423.  
  424. NODEP
  425. decl_enum()
  426. {
  427.     NODEP bas_type();
  428.  
  429.     fadvnode();     /* skip 'enum' */
  430.  
  431.     if (cur->e_token == ID) {       /* ignore tag */
  432.         fadvnode();
  433.     }
  434.     if (cur->e_token == '{') {      /* new declaration */
  435.         fadvnode();     /* skip '{' */
  436.         en_decls();     /* global scope */
  437.         if (cur->e_token != '}')
  438.             error("expect }");
  439.         else
  440.             fadvnode();     /* skip '}' */
  441.     }
  442.     return bas_type(K_INT);
  443. }
  444.  
  445. extern lineno;
  446.  
  447. NODEP
  448. decl_su(isunion)
  449. {
  450.     register NODEP rv, tagp;
  451.     NODEP *attab;
  452.  
  453.     fadvnode();     /* skip 'struct' or 'union' */
  454.  
  455.     attab = level ? &blktab->b_tags : &tagtab;
  456.     tagp = NULL;
  457.     if (cur->e_token == ID) {       /* hold on to ID node */
  458.         tagp = cur;
  459.         e_to_t(tagp);
  460.         advnode();
  461.         nnmadd(tagp, isunion ? ".U" : ".S");
  462.     }
  463.     if (cur->e_token == '{') {      /* new declaration */
  464.         if (tagp == NULL) {     /* make fake name */
  465.             tagp = allocnode();
  466.             sprintf(tagp->n_name, isunion ? "%dU" :
  467.                     "%dS", lineno);
  468.         }
  469.         fadvnode();     /* skip '{' */
  470.         if (rv = llook(*attab, tagp)) {
  471.             freenode(tagp);
  472.             if (rv->n_right) {
  473.                 errors("struct redefined", rv->n_name);
  474.                 freenode(rv->n_right);
  475.                 rv->n_right = NULL;
  476.             }
  477.         } else {        /* new defn */
  478.             rv = tagp;
  479.             rv->t_token = isunion ? K_UNION : K_STRUCT;
  480.             rv->n_flags |= N_BRKPR; /* break print loops */
  481.             putlist(attab, rv);
  482.         }
  483.         su_decls(&rv->n_right, isunion,
  484.                 &rv->t_size, &rv->t_aln);
  485.         if (cur->e_token != '}')
  486.             error("expect }");
  487.         else
  488.             fadvnode();     /* skip '}' */
  489.     } else {        /* reference to old */
  490.         if (tagp == NULL) {
  491.             error("nonsense struct");
  492.             goto out;
  493.         }
  494.         /* ANSI special decl
  495.             struct <tag> ;
  496.            for hiding old tag within block */
  497.         if (cur->e_token == ';' && level)
  498.             rv = llook(*attab, tagp);
  499.         else
  500.             rv = alltags(tagp);
  501.         if (rv == NULL) {       /* delayed tag */
  502.             rv = tagp;
  503.             rv->t_token = isunion ? K_UNION : K_STRUCT;
  504.             rv->n_flags |= N_BRKPR; /* break print loops */
  505.             putlist(attab, rv);
  506.             goto out;
  507.         } else
  508.             freenode(tagp);
  509.     }
  510. out:
  511.     return rv;
  512. }
  513.  
  514. NODE *
  515. alltags(np)
  516. NODE *np;
  517. {
  518.     register NODE *bp;
  519.     NODE *rv;
  520.  
  521.     for (bp=blktab; bp != NULL; bp = bp->n_next)
  522.         if ((rv = llook(bp->b_tags, np)) != NULL)
  523.             return rv;
  524.     return llook(tagtab, np);
  525. }
  526.  
  527. NODE *
  528. allsyms(np)
  529. NODE *np;
  530. {
  531.     register NODE *bp;
  532.     NODE *rv;
  533.  
  534.     for (bp=blktab; bp != NULL; bp = bp->n_next)
  535.         if ((rv = llook(bp->b_syms, np)) != NULL)
  536.             return rv;
  537.     return hlook(symtab, np);
  538. }
  539.  
  540. sim_type(a,b)
  541. register NODE *a, *b;
  542. {
  543. more:
  544.     if (a == b)
  545.         return 1;
  546.     if (a == NULL || b == NULL)
  547.         return 0;
  548.     if (a->t_token != b->t_token)
  549.         return 0;
  550.     if (a->t_size != b->t_size && a->t_size && b->t_size)
  551.         return 0;
  552.     a = a->n_tptr;
  553.     b = b->n_tptr;
  554.     goto more;
  555. }
  556.  
  557. /* 2nd def of same name at same level */
  558. /* OK if one extern and types the same */
  559. def2nd(old,new)
  560. NODEP old, new;
  561. {
  562.     int osc, nsc;
  563.  
  564.     if (sim_type(old->n_tptr, new->n_tptr) == 0)
  565.         goto bad;
  566.     osc = old->e_sc;
  567.     nsc = new->e_sc;
  568.     if (nsc == K_EXTERN) {  /* works only if no further use allowed */
  569.         freenode(new);
  570.         return 0;
  571.     }
  572.     if (osc == K_EXTERN) {
  573.         /* replace old def with new one */
  574.         /* for now, just put new one on list too */
  575.         return 1;
  576.     }
  577. bad:
  578.     errorn("bad 2nd decl of ", new);
  579.     /* use 2nd def so other stuff works */
  580.     return 1;
  581. }
  582.  
  583. /* saw fun but no body */
  584. fix_fun(np)
  585. NODE *np;
  586. {
  587.     if (np == NULL) return;
  588.     if (np->n_tptr->t_token == '(') {       /* fix to extern */
  589.         if (np->e_sc != K_TYPEDEF)
  590.             np->e_sc = K_EXTERN;
  591.     }
  592. }
  593.  
  594. e_to_t(np)
  595. NODE *np;
  596. {
  597.     int token;
  598.  
  599.     token = np->e_token;
  600.     np->t_token = token;
  601.     np->t_size = 0;
  602.     np->t_aln = 0;
  603. }
  604.